package com.alphacsp.android;

import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.provider.MediaStore;

import java.io.File;

/**
 * @author Yoav Aharoni
 */
public class SetupActivity extends PreferenceActivity implements Preference.OnPreferenceChangeListener {
	private SharedPreferences preferences;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		preferences = PreferenceManager.getDefaultSharedPreferences(this);

		addPreferencesFromResource(R.xml.preferences);

		findPreference("fighterImage1").setOnPreferenceChangeListener(this);
		findPreference("fighterImage2").setOnPreferenceChangeListener(this);
	}

	public boolean onPreferenceChange(Preference preference, Object newValue) {
		if (Boolean.TRUE.equals(newValue)) {
			final File photoFile = new File(Environment.getExternalStorageDirectory(), preference.getKey());
			final Uri photoUri = Uri.fromFile(photoFile);
			startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE)
					.putExtra(MediaStore.EXTRA_OUTPUT, photoUri), 123);
		}
		return true;
	}
}
